﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Gurucore.Framework.Core;
using Gurucore.Framework.DataAccess.Persistence.QueryLanguage;

namespace Gurucore.Framework.DataAccess.Persistence
{
	public class JoinQuery<T, U> : Query<T> where T : class where U : class
	{
		private Query<T> m_left;
		private Query<U> m_right;
		private Expression m_expOn;
		private JoinType m_joinType;

		public enum JoinType
		{
			Inner,
			LeftOuter
		}

		internal JoinQuery(Query<T> p_left, Query<U> p_right, Expression p_expOn, JoinType p_joinType)
		{
			m_left = p_left;
			m_right = p_right;
			m_expOn = p_expOn;
			m_joinType = p_joinType;
		}

		internal override string GetQueryTarget()
		{
			string sJoin = (m_joinType == JoinQuery<T, U>.JoinType.Inner) ? "INNER JOIN" : "LEFT OUTER JOIN";
			string sTarget = string.Format("({0} {1} {2} ON {3})", m_left.GetQueryTarget(), sJoin, m_right.GetQueryTarget(), m_expOn.ToString());
			return sTarget;
		}

		internal override List<string> GetColumnsList()
		{
			TableInfoManager oTableInfoMgr = Application.GetInstance().GetGlobalSharedObject<TableInfoManager>();
			TableInfo oTableInfo = oTableInfoMgr.GetTableInfo(typeof(T));

			return oTableInfo.Column;
		}

		internal override T GetSample()
		{
			return default(T);
		}

		internal override DTOTransformer GetBatchTransformer()
		{
			return new SingleDTOTransformer();
		}

		public virtual JoinQuery<T, U> GroupBy(Expression groupExp)
		{
			return this;
		}
	}
}
